﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using JobBoardAdminPanel.Helpers;
using JobBoardAdminPanel.Commands;

namespace JobBoardAdminPanel.ViewModels
{
    public class EditCountriesViewModel : ViewModelBase
    {
        private ObservableCollection<CountryViewModel> countries;
        private CountryViewModel currentCountry;
        private CountryViewModel newCountry = new CountryViewModel(new JobsBoardDAL.DTOs.CountryDTO());
        private SaveCountryCommand saveCommand;
        private DeleteCountryCommand deleteCommand;
        private AddCountryCommand addCommand;

        public EditCountriesViewModel()
        {
            RefreshCountries();
            saveCommand = new SaveCountryCommand(this);
            deleteCommand = new DeleteCountryCommand(this);
            addCommand = new AddCountryCommand(this);
        }
  
        public AddCountryCommand AddCommand
        {
            get
            {
                return addCommand;
            }
            set
            {
                addCommand = value;
            }
        }

        public void Add()
        {
            this.NewCountry.Add();
            RefreshCountries();
        }

        public CountryViewModel NewCountry
        {
            get
            {
                return newCountry;
            }
            set
            {
                if (this.NewCountry != value)
                {
                    newCountry = value;
                    OnPropertyChanged("NewCountry");
                }
            }
        }

        public SaveCountryCommand SaveCommand
        {
            get
            {
                return saveCommand;
            }
            set
            {
                saveCommand = value;
            }
        }

        public DeleteCountryCommand DeleteCommand
        {
            get
            {
                return deleteCommand;
            }
            set
            {
                deleteCommand = value;
            }
        }

        public void RefreshCountries()
        {
            this.Countries = new ObservableCollection<CountryViewModel>(ViewModelsHelper.GetAllCountries());
        }

        public CountryViewModel CurrentCountry
        {
            get
            {
                return currentCountry;
            }
            set
            {
                if (this.CurrentCountry != value)
                {
                    currentCountry = value;
                    OnPropertyChanged("CurrentCountry");
                }
            }
        }

        public ObservableCollection<CountryViewModel> Countries
        {
            get
            {
                return countries;
            }
            set
            {
                if (this.countries != value)
                {
                    countries = value;
                    OnPropertyChanged("Countries");
                }
            }
        }

        internal void Update()
        {
            if (this.CurrentCountry != null)
            {
                this.CurrentCountry.Update();
                RefreshCountries();
                this.CurrentCountry = null;
            }
        }

        internal void Delete()
        {
            if (this.CurrentCountry != null)
            {
                this.CurrentCountry.Delete();
                RefreshCountries();
            }
        }
    }
}